/**
 * Copyright &copy; 2016-2017 <a href="https://www.lczyfz.com">联创智云</a> All rights reserved.
 */
package com.lczyfz.campusinfo.modules.jobmesmanage.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lczyfz.campusinfo.common.utils.Const;
import com.lczyfz.campusinfo.modules.jobmesmanage.service.InetAddressService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.lczyfz.campusinfo.common.config.Global;
import com.lczyfz.campusinfo.common.persistence.Page;
import com.lczyfz.campusinfo.common.web.BaseController;
import com.lczyfz.campusinfo.common.utils.StringUtils;
import com.lczyfz.campusinfo.modules.jobmesmanage.entity.JobMes;
import com.lczyfz.campusinfo.modules.jobmesmanage.service.JobMesService;

import java.net.InetAddress;

/**
 * 就业信息Controller
 * @author zms
 * @version 2018-03-23
 */
@Controller
@RequestMapping(value = "${adminPath}/jobmesmanage/jobMes")
public class JobMesController extends BaseController {

	@Autowired
	private JobMesService jobMesService;
	@ModelAttribute
	public JobMes get(@RequestParam(required=false) String id) {
		JobMes entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = jobMesService.get(id);
		}
		if (entity == null){
			entity = new JobMes();
		}
		return entity;
	}
	
	@RequiresPermissions("jobmesmanage:jobMes:view")
	@RequestMapping(value = {"list", ""})
	public String list(JobMes jobMes, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<JobMes> page = jobMesService.findPage(new Page<JobMes>(request, response), jobMes); 
		model.addAttribute("page", page);
		return "modules/jobmesmanage/jobMesList";
	}

	@RequiresPermissions("jobmesmanage:jobMes:view")
	@RequestMapping(value = "form")
	public String form(JobMes jobMes, Model model) {
		model.addAttribute("jobMes", jobMes);
		return "modules/jobmesmanage/jobMesForm";
	}

	@RequiresPermissions("jobmesmanage:jobMes:edit")
	@RequestMapping(value = "save")
	public String save(JobMes jobMes, HttpServletRequest request, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, jobMes)){
			return form(jobMes, model);
		}
		if (StringUtils.isNotBlank(jobMes.getJobImg())){
			jobMes.setJobImg(Const.getinetAddressService(jobMes.getJobImg(),request));
		}
		jobMesService.save(jobMes);
		addMessage(redirectAttributes, "保存就业信息成功");
		return "redirect:"+Global.getAdminPath()+"/jobmesmanage/jobMes/?repage";
	}
	
	@RequiresPermissions("jobmesmanage:jobMes:edit")
	@RequestMapping(value = "delete")
	public String delete(JobMes jobMes, RedirectAttributes redirectAttributes) {
		jobMesService.delete(jobMes);
		addMessage(redirectAttributes, "删除就业信息成功");
		return "redirect:"+Global.getAdminPath()+"/jobmesmanage/jobMes/?repage";
	}

}